import { PraiseButton,Thumb,Star } from "./praise-button.es";
import '../css/praisebutton.css';
import '../css/starbutton.css';
xtag.register('x-praise', {
  lifecycle: {
    created: function () {
      this.init(80);
    }
  },
  methods: {
    init: function (width) {
      var btn = new Thumb(this);
      btn.buttonWidth = width;


      //根据后台数据，初始化PraiseButton
      console.time('get praise count');
      axios.get('/index/count')
        .then(function  (response)  {
          let result = response.data;
          console.log(result);
          if (result.success) {
            btn.init(result.result);
          }
          else {
            alert(result.errorMsg);
          }
          console.timeEnd('get praise count');
        })
        .catch(function  (error)  {
          console.log(error);
        });

      //PraiseButton自增时，后台数据同时自增
      btn.incEventHandle = function () {
        axios.post('/index/index')
          .then(function  (response)  {
            console.log(response.data);
          })
          .catch(function  (error)  {
            console.log(error);
          });
      }
    }
  }
}); 

xtag.register('x-star', {
  lifecycle: {
    created: function () {
      this.init(80);
    }
  },
  methods: {
    init: function (width) {
      var btn = new Star(this);
      btn.buttonWidth = width;


      //根据后台数据，初始化PraiseButton
      console.time('get praise count');
      axios.get('/index/count')
        .then(function  (response)  {
          let result = response.data;
          console.log(result);
          if (result.success) {
            btn.init(result.result);
          }
          else {
            alert(result.errorMsg);
          }
          console.timeEnd('get praise count');
        })
        .catch(function  (error)  {
          console.log(error);
        });

      //PraiseButton自增时，后台数据同时自增
      btn.incEventHandle = function () {
        axios.post('/index/index')
          .then(function  (response)  {
            console.log(response.data);
          })
          .catch(function  (error)  {
            console.log(error);
          });
      }
    }
  }
}); 

